//@ts-nocheck
import React, { useEffect, useState, useReducer } from 'react';
const initialState = 0;

function reducer(state: any, action: any) {
  switch (action.type) {
  case 'increment':
    return { number: state.number + 1 };
  case 'decrement':
    return { number: state.number - 1 };
  default:
    throw new Error();
  }
}
function init(initialState: any) {
  return { number: initialState };
}

//用新的dispatch替代老的dispatch
function useThunk(reducer: any, initialState: any, init: any) {
  const [state, dispatch] = useReducer(reducer, initialState, init);
  const thunkDispatch = (action: any) => {
    if (typeof action === 'function') {
      action(thunkDispatch, () => state)
    } else {
      dispatch(action)
    }
  };

  return [state, thunkDispatch];
}

export default function Counter() {
  const [state, dispatch] = useThunk(reducer, initialState, init);
  return (
    <>
            //@ts-ignore
            Count: {state.number}
            //@ts-ignore
      <button onClick={()=>dispatch(function(dispatch,getState){
        dispatch({type:'increment'})
      })}>+</button>
            //@ts-ignore
      <button onClick={() => dispatch({ type: 'decrement' })}>-</button>
    </>
  )
}
